package net.wanho.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSSClient;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.service.FileService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * Author：汤小洋
 * Date：2025-09-29 10:04
 * Description：<描述>
 */
@Service
public class FileServiceImpl implements FileService {

    @Resource
    private OSSClient ossClient;

    @Value("${spring.cloud.alicloud.oss.bucket}")
    private String bucket;

    @Value("${spring.cloud.alicloud.oss.endpoint}")
    private String endpoint;

    @SneakyThrows
    @Override
    public String upload(MultipartFile file) {
        String dir = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String objectName = dir + "/" + IdUtil.simpleUUID() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        ossClient.putObject(bucket, objectName, file.getInputStream());

        String url = "https://"+bucket+"."+endpoint+"/"+objectName;
        return url;
    }
}
